<?php

class Users_Model_Access_Group_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract {

	protected $_objectClass 		= 'Users_Model_Access_Group';

	protected $_tableClass 			= 'Users_Table_Access_Group';

	protected $_iteratorClass 		= 'Workset_Model_Object_Iterator';

    protected $_roleToGroupTableClass = 'Users_Table_Access_Role2Group';

    protected $_userToGroupTableClass = 'Users_Table_Access_User2Group';

	function linkToObject(Workset_Model_Object_Interface $object, Workset_Model_Object_Interface $refObject) {
        $data = $object->getData();

        if (!$data instanceof Workset_Db_Table_Row) {
            throw new Zend_Exception('Invalid object data type');
        }

        $tableClass = null;

        switch (true) {
            case $refObject instanceof Users_Model_Access_Role:
                $tableClass = $this->_roleToGroupTableClass;
                $field = 'role_id';
            break;

            case $refObject instanceof Users_Model_User:
                $tableClass = $this->_userToGroupTableClass;
                $field = 'user_id';
            break;

            default:
                throw new Zend_Exception('Unknown reference object type');
        }

        if ($data instanceof Workset_Db_Table_Row) {
            return $data->addDependentRow($tableClass, array(
                $field => $refObject->getId()
            ));
        }

    }

    function unlinkObject(Workset_Model_Object_Interface $object, Workset_Model_Object_Interface $refObject) {
        $data = $object->getData();

        if (!$data instanceof Workset_Db_Table_Row) {
            throw new Zend_Exception('Invalid object data type');
        }

        $tableClass = null;

        switch (true) {
            case $refObject instanceof Users_Model_Access_Role:
                $tableClass = $this->_roleToGroupTableClass;
                $field = 'role_id';
            break;

            case $refObject instanceof Users_Model_User:
                $tableClass = $this->_userToGroupTableClass;
                $field = 'user_id';
            break;

            default:
                throw new Zend_Exception('Unknown reference object type');
        }

        $subrow = $data->findDependentRow($tableClass, 'group', array(
            "$field = ?" => $refObject->getId()
        ));
        if ($subrow) {
            return $subrow->delete();
        }

    }

    function getUserGroups(Users_Model_User $user) {

        $select = $this->_getTable()->select();

        $select->from(
            array('m' => $this->_getTable()->info(Zend_Db_Table_Abstract::NAME))
        )
       ->join(
            array('i' => $this->_getTable($this->_userToGroupTableClass)->info(Zend_Db_Table_Abstract::NAME)),
            "i.group_id = m.id",
            array()
        )
        ->where(
            'i.user_id = ?', $user->getId()
        );

        $rowset = $this->_getTable()->fetchAll($select);

        return $this->getIterator($rowset, array(
            'countRows' => $this->_countAllRecords,
            'filter' => $select
        ));
    }

}

?>